﻿using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentEscalator.DLL
{

    //结构体属性（一字节对齐），不写的话默认4字节对齐
    //[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
    public struct hw_version_c
    {
        public byte pcb_ver;  // PCB  version.
        public byte bom_ver;  // BOM  version.
                              //C#结构体中的数组必须规定他的大小，SizeConst数组大小
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public byte[] fw_ver;
        public IntPtr fw_ver_str;
        public IntPtr fw_BuildTime;
    };

    public struct hw_version
    {
        public byte pcb_ver;  // PCB  version.
        public byte bom_ver;  // BOM  version.
                              //C#结构体中的数组必须规定他的大小，SizeConst数组大小
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public byte[] fw_ver;
        //定义字符串时需定义字符串的长度，SizeConst为字符串的长度
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string fw_ver_str;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string fw_BuildTime;
    };

    struct dll_version_c
    {
        public int major_ver;
        public int minor_ver;
        public int micro_ver;
        public IntPtr str;
    }

    struct dll_version
    {
        public int major_ver;
        public int minor_ver;
        public int micro_ver;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string desc_str;
    }
    class _libdaq
    {
        /*
         * library raleted API
         */
        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_init", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_init();

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_exit", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_exit();

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_set_option", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_set_option(int option);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_get_version", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern void libdaq2_get_version(IntPtr dll_version);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_get_error_desc", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern IntPtr libdaq2_get_error_desc(int error_code);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_get_error_str", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern IntPtr libdaq2_get_error_str(int error_code);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_device_get_count", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_device_get_count();

        /*
         * device Manage raleted API
         */
        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_device_get_sn", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_device_get_sn(uint index, byte[] device_sn, int length);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_device_get_version", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_device_get_version(byte[] device_name_sn, IntPtr hw_version);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_device_get_name", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_device_get_name(byte[] device_sn, byte[] device_name, int length);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_device_get_model", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_device_get_model(byte[] device_name_sn, byte[] device_model, int length);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_device_get_status", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_device_get_status(byte[] device_name_sn);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_device_reboot", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_device_reboot(byte[] device_name_sn);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_device_open", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_device_open(byte[] device_name_sn);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_device_close", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_device_close(byte[] device_name_sn);


        /*
         *  Common API
         */
        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_send_command", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_send_command(byte[] device_name_sn, byte[] module_name, byte[] cmd);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_set_propertyString", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_set_propertyString(byte[] device_name_sn, byte[] module_name, byte[] property, byte[] strValue);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_get_propertyString", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_get_propertyString(byte[] device_name_sn, byte[] module_name, byte[] property, byte[] strValue, int strSize);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_set_propertyDouble", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_set_propertyDouble(byte[] device_name_sn, byte[] module_name, byte[] property, double value);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_get_propertyDouble", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_get_propertyDouble(byte[] device_name_sn, byte[] module_name, byte[] property, ref double value);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_set_propertyInt", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_set_propertyInt(byte[] device_name_sn, byte[] module_name, byte[] property, int value);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_get_propertyInt", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_get_propertyInt(byte[] device_name_sn, byte[] module_name, byte[] property, ref int value);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_set_propertyDoubleArrary", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_set_propertyDoubleArrary(byte[] device_name_sn, byte[] module_name, byte[] property, double[] arrayValue, int arraySize);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_get_propertyDoubleArrary", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_get_propertyDoubleArrary(byte[] device_name_sn, byte[] module_name, byte[] property, double[] arrayValue, int arraySize);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_set_propertyIntArrary", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_set_propertyIntArrary(byte[] device_name_sn, byte[] module_name, byte[] property, int[] arrayValue, int arraySize);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_get_propertyIntArrary", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_get_propertyIntArrary(byte[] device_name_sn, byte[] module_name, byte[] property, int[] arrayValue, int arraySize);
        /*
         * DIO module struct and API definition
         */
        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_dio_get_iocount", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_dio_get_iocount(byte[] device_name, byte[] module_name, ref byte iocount);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_dio_write_bit", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_dio_write_bit(byte[] device_name, byte[] module_name, byte ioIndex, byte BitVal);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_dio_write_port", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_dio_write_port(byte[] device_name, byte[] module_name, byte[] PortVal);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_dio_read_bit", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_dio_read_bit(byte[] device_name, byte[] module_name, byte ioIndex, ref byte BitVal);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_dio_read_port", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_dio_read_port(byte[] device_name, byte[] module_name, byte[] PortVal);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_dio_set_callback", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_dio_set_callback(byte[] device_name, byte[] module_name, byte ioIndex, byte io_event, IntPtr fn_callback, IntPtr userParam);
        /*
         * DAC module struct and API definition
         */

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_dac_set_value", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_dac_set_value(byte[] device_name, byte[] module_name, double value);

        /*
         * ADC module struct and API definition
         */
        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_adc_sync_channelsetting", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_adc_sync_channelsetting(byte[] deviceName, byte[] module_name);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_adc_single_Sample", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_adc_single_Sample(byte[] deviceName, byte[] module_name, double[] resultbuf, int bufsize);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_adc_clear_buffer", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_adc_clear_buffer(byte[] DeviceName, byte[] module_name);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_adc_resize_buffer", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_adc_resize_buffer(byte[] DeviceName, byte[] module_name, int newsize);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_adc_read_analog", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_adc_read_analog(byte[] DeviceName, byte[] module_name, double[] databuf, uint buflen, ref uint actuallen);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_adc_read_binary", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_adc_read_binary(byte[] DeviceName, byte[] module_name, int[] databuf, uint buflen, ref uint actuallen);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_adc_read_raw", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_adc_read_raw(byte[] DeviceName, byte[] module_name, byte[] databuf, uint buflen, ref uint actuallen);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_adc_read_analog_sync", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_adc_read_analog_sync(byte[] DeviceName, byte[] module_name, double[] databuf, uint buflen, ref uint actuallen, int timeout);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_adc_read_binary_sync", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_adc_read_binary_sync(byte[] DeviceName, byte[] module_name, int[] databuf, uint buflen, ref uint actuallen, int timeout);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_adc_extract_ChannelAnalogData", CallingConvention = CallingConvention.StdCall)]
        public static extern void libdaq2_adc_extract_ChannelAnalogData(double[] all_databuf, uint all_datalen, byte ch_listlen, byte ch_index, double[] ch_databuf);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_adc_extract_ChannelBinaryData", CallingConvention = CallingConvention.StdCall)]
        public static extern void libdaq2_adc_extract_ChannelBinaryData(int[] all_databuf, uint all_datalen, byte ch_listlen, byte ch_index, int[] ch_databuf);

        /*
        * Counter module struct and API definition
        */

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_cnt_get_value", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_cnt_get_value(byte[] device_name, byte[] module_name, ref int value);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_cnt_read_buffer", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_cnt_read_buffer(byte[] DeviceName, byte[] module_name, int[] countbuf, uint buflen, ref uint actuallen);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_cnt_clear_buffer", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_cnt_clear_buffer(byte[] DeviceName, byte[] module_name);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_cnt_set_callback", CallingConvention = CallingConvention.StdCall)]
        public static extern int libdaq2_cnt_set_callback(byte[] device_name, byte[] module_name, IntPtr fn_callback, IntPtr userParam);

        [DllImport(@"daqlib2.dll", EntryPoint = "libdaq2_cnt_to_pwm", CallingConvention = CallingConvention.StdCall)]
        public static extern void libdaq2_cnt_to_pwm(int[] countbuf, IntPtr pwm_buf, uint size);

    }

    class libdaq2
    {
        public const int LIBDAQ_ERROR_TIMEOUT = -7;
        public const byte LIBDAQ_ACCESS_MODE_DEV_NAME_SN_BOTH = 0x01;//default
        public const byte LIBDAQ_ACCESS_MODE_DEV_NAME_ONLY = 0x01;
        public const byte LIBDAQ_ACCESS_MODE_DEV_SN_ONLY = 0x02;

        public static int libdaq2_init() { return _libdaq.libdaq2_init(); }

        public static void libdaq2_exit() { _libdaq.libdaq2_exit(); }

        public static void libdaq2_set_option(int option) { _libdaq.libdaq2_set_option(option); }

        public static dll_version libdaq2_get_version()
        {

            dll_version_c _dll_version_c;//C 类型用于和dll通信
            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(dll_version_c))); //非托管内存中分配内存
            _libdaq.libdaq2_get_version(ptr);
            _dll_version_c = (dll_version_c)Marshal.PtrToStructure(ptr, typeof(dll_version_c));//将数据从非托管内存块封送到托管对象

            dll_version _version = new dll_version();//用于返回
            _version.major_ver = _dll_version_c.major_ver;
            _version.micro_ver = _dll_version_c.micro_ver;
            _version.minor_ver = _dll_version_c.minor_ver;
            _version.desc_str = Marshal.PtrToStringAnsi(_dll_version_c.str);
            return _version;
        }

        public static int libdaq2_device_get_count() { return _libdaq.libdaq2_device_get_count(); }

        public static string libdaq2_device_get_sn(uint index)
        {
            byte[] device_sn = new byte[32];
            int error_code = _libdaq.libdaq2_device_get_sn(index, device_sn, 32);
            parse_error(error_code);
            return System.Text.Encoding.Default.GetString(device_sn).TrimEnd('\0');
        }

        public static void parse_error(int error_code)
        {
            if (error_code != 0)
            {
                string error_str, error_desc;
                IntPtr ptr_desc = _libdaq.libdaq2_get_error_desc(error_code);
                IntPtr ptr_str = _libdaq.libdaq2_get_error_str(error_code);

                error_str = Marshal.PtrToStringAnsi(ptr_str);
                error_desc = Marshal.PtrToStringAnsi(ptr_desc);

                throw new Exception(String.Format("error code:{0},{1},{2}", error_code, error_str, error_desc));
            }
        }
    }

    /*
    * device manage struct and API definition
    */
    public class libdaq2_device
    {
        private byte[] m_device_sn;

        public libdaq2_device(byte[] device_sn)
        {
            this.m_device_sn = device_sn;
        }

        public hw_version device_get_version()
        {

            hw_version_c _hw_version_c;//C 类型用于和dll通信
            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(hw_version_c))); //非托管内存中分配内存
            int error_code = _libdaq.libdaq2_device_get_version(this.m_device_sn, ptr);
            libdaq2.parse_error(error_code);
            _hw_version_c = (hw_version_c)Marshal.PtrToStructure(ptr, typeof(hw_version_c));//将数据从非托管内存块封送到托管对象

            hw_version _hw_version = new hw_version();//用于返回
            _hw_version.bom_ver = _hw_version_c.bom_ver;
            _hw_version.pcb_ver = _hw_version_c.pcb_ver;
            _hw_version.fw_ver = _hw_version_c.fw_ver;
            _hw_version.fw_ver_str = Marshal.PtrToStringAnsi(_hw_version_c.fw_ver_str);
            _hw_version.fw_BuildTime = Marshal.PtrToStringAnsi(_hw_version_c.fw_BuildTime);

            return _hw_version;
        }

        public string device_get_name()
        {
            byte[] device_name = new byte[32];
            int error_code = _libdaq.libdaq2_device_get_name(this.m_device_sn, device_name, 32);
            libdaq2.parse_error(error_code);
            return System.Text.Encoding.Default.GetString(device_name).TrimEnd('\0');
        }

        public string device_get_model()
        {
            byte[] device_model = new byte[32];
            int error_code = _libdaq.libdaq2_device_get_model(this.m_device_sn, device_model, 32);
            libdaq2.parse_error(error_code);
            return System.Text.Encoding.Default.GetString(device_model).TrimEnd('\0');
        }

        public int device_get_status()
        {
            return _libdaq.libdaq2_device_get_status(this.m_device_sn);
        }

        public void device_reboot()
        {
            int error_code = _libdaq.libdaq2_device_reboot(this.m_device_sn);
            libdaq2.parse_error(error_code);
        }

        public void device_open()
        {
            int error_code = _libdaq.libdaq2_device_open(this.m_device_sn);
            libdaq2.parse_error(error_code);
        }

        public void device_close()
        {
            int error_code = _libdaq.libdaq2_device_close(this.m_device_sn);
            libdaq2.parse_error(error_code);
        }
    }

    public class libdaq2_common
    {
        private byte[] m_device_name;
        private byte[] m_module_name;

        public libdaq2_common(byte[] device_name, byte[] module_name)
        {
            this.m_device_name = device_name;
            this.m_module_name = module_name;
        }

        public void send_command(string cmd)
        {
            byte[] _cmd = Encoding.Default.GetBytes(cmd);
            int error_code = _libdaq.libdaq2_send_command(m_device_name, m_module_name, _cmd);
            libdaq2.parse_error(error_code);
        }

        public void set_propertyString(string property, string strValue)
        {
            byte[] _property = Encoding.Default.GetBytes(property);
            byte[] _strValue = Encoding.Default.GetBytes(strValue);
            int error_code = _libdaq.libdaq2_set_propertyString(m_device_name, m_module_name, _property, _strValue);
            libdaq2.parse_error(error_code);
        }

        public string get_propertyString(string property)
        {
            byte[] _property = Encoding.Default.GetBytes(property);
            byte[] _strValue = new byte[255];
            int error_code = _libdaq.libdaq2_get_propertyString(m_device_name, m_module_name, _property, _strValue, 255);
            libdaq2.parse_error(error_code);

            return System.Text.Encoding.Default.GetString(_strValue).TrimEnd('\0');
        }
        public void set_propertyDouble(string property, double value)
        {
            byte[] _property = Encoding.Default.GetBytes(property);
            int error_code = _libdaq.libdaq2_set_propertyDouble(m_device_name, m_module_name, _property, value);
            libdaq2.parse_error(error_code);
        }
        public double get_propertyDouble(string property)
        {
            double value = 0;
            byte[] _property = Encoding.Default.GetBytes(property);
            int error_code = _libdaq.libdaq2_get_propertyDouble(m_device_name, m_module_name, _property, ref value);
            libdaq2.parse_error(error_code);

            return value;
        }

        public void set_propertyInt(string property, int value)
        {
            byte[] _property = Encoding.Default.GetBytes(property);
            int error_code = _libdaq.libdaq2_set_propertyInt(m_device_name, m_module_name, _property, value);
            libdaq2.parse_error(error_code);
        }
        public int get_propertyInt(string property)
        {
            int value = 0;
            byte[] _property = Encoding.Default.GetBytes(property);
            int error_code = _libdaq.libdaq2_get_propertyInt(m_device_name, m_module_name, _property, ref value);
            libdaq2.parse_error(error_code);

            return value;
        }

        public void set_propertyDoubleArrary(string property, double[] arrayValue)
        {
            byte[] _property = Encoding.Default.GetBytes(property);
            int error_code = _libdaq.libdaq2_set_propertyDoubleArrary(m_device_name, m_module_name, _property, arrayValue, arrayValue.Length);
            libdaq2.parse_error(error_code);
        }
        public double[] get_propertyDoubleArrary(string property, int size)
        {
            double[] value = new double[size];
            byte[] _property = Encoding.Default.GetBytes(property);
            int error_code = _libdaq.libdaq2_get_propertyDoubleArrary(m_device_name, m_module_name, _property, value, size);
            libdaq2.parse_error(error_code);

            return value;
        }

        public void set_propertyIntArrary(string property, int[] arrayValue)
        {
            byte[] _property = Encoding.Default.GetBytes(property);
            int error_code = _libdaq.libdaq2_set_propertyIntArrary(m_device_name, m_module_name, _property, arrayValue, arrayValue.Length);
            libdaq2.parse_error(error_code);
        }
        public int[] get_propertyIntArrary(string property, int size)
        {
            int[] value = new int[size];
            byte[] _property = Encoding.Default.GetBytes(property);
            int error_code = _libdaq.libdaq2_get_propertyIntArrary(m_device_name, m_module_name, _property, value, size);
            libdaq2.parse_error(error_code);

            return value;
        }
    }


    public class libdaq2_adc : libdaq2_common
    {
        private byte[] m_device_name_sn;
        private byte[] m_module_name;

        public libdaq2_adc(byte[] device_name_sn, byte[] module_name) : base(device_name_sn, module_name)
        {
            this.m_device_name_sn = device_name_sn;
            this.m_module_name = module_name;
        }

        public void sync_channelsetting()
        {
            int error_code = _libdaq.libdaq2_adc_sync_channelsetting(m_device_name_sn, m_module_name);
            libdaq2.parse_error(error_code);
        }

        public double[] single_Sample(int size)
        {
            double[] databuf = new double[size];
            int error_code = _libdaq.libdaq2_adc_single_Sample(m_device_name_sn, m_module_name, databuf, size);
            libdaq2.parse_error(error_code);
            return databuf;
        }

        public void clear_buffer()
        {
            int error_code = _libdaq.libdaq2_adc_clear_buffer(m_device_name_sn, m_module_name);
            libdaq2.parse_error(error_code);
        }

        public void resize_buffer(int newsize)
        {
            int error_code = _libdaq.libdaq2_adc_resize_buffer(m_device_name_sn, m_module_name, newsize);
            libdaq2.parse_error(error_code);
        }

        public void read_analog(double[] databuf, uint buflen, ref uint actuallen)
        {
            int error_code = _libdaq.libdaq2_adc_read_analog(m_device_name_sn, m_module_name, databuf, buflen, ref actuallen);
            libdaq2.parse_error(error_code);
        }

        public void read_binary(int[] databuf, uint buflen, ref uint actuallen)
        {
            int error_code = _libdaq.libdaq2_adc_read_binary(m_device_name_sn, m_module_name, databuf, buflen, ref actuallen);
            libdaq2.parse_error(error_code);
        }

        public void read_raw(byte[] databuf, uint buflen, ref uint actuallen)
        {
            int error_code = _libdaq.libdaq2_adc_read_raw(m_device_name_sn, m_module_name, databuf, buflen, ref actuallen);
            libdaq2.parse_error(error_code);
        }

        public void read_analog_sync(double[] databuf, uint buflen, ref uint actuallen, int timeout)
        {
            int error_code = _libdaq.libdaq2_adc_read_analog_sync(m_device_name_sn, m_module_name, databuf, buflen, ref actuallen, timeout);
            if (error_code != libdaq2.LIBDAQ_ERROR_TIMEOUT) //忽略time out
                libdaq2.parse_error(error_code);
        }

        public void read_binary_sync(int[] databuf, uint buflen, ref uint actuallen, int timeout)
        {
            int error_code = _libdaq.libdaq2_adc_read_binary_sync(m_device_name_sn, m_module_name, databuf, buflen, ref actuallen, timeout);
            if (error_code != libdaq2.LIBDAQ_ERROR_TIMEOUT) //忽略time out
                libdaq2.parse_error(error_code);
        }

        public void extract_ChannelAnalogData(double[] all_databuf, uint all_datalen, byte ch_listlen, byte ch_index, double[] ch_databuf)
        {
            for (uint i = 0; i < all_datalen / ch_listlen; i++)
            {
                ch_databuf[i] = double.Parse(all_databuf[i * ch_listlen + ch_index].ToString("F10"));
            }
        }

        public void extract_ChannelBinaryData(int[] all_databuf, uint all_datalen, byte ch_listlen, byte ch_index, int[] ch_databuf)
        {
            for (uint i = 0; i < all_datalen / ch_listlen; i++)
            {
                ch_databuf[i] = all_databuf[i * ch_listlen + ch_index];
            }
        }
    }
    public class DAQ7100 : libdaq2_device
    {
        public libdaq2_adc ADC;

        public DAQ7100(string device_name_sn) : base(Encoding.Default.GetBytes(device_name_sn))
        {
            this.ADC = new libdaq2_adc(Encoding.Default.GetBytes(device_name_sn), Encoding.Default.GetBytes("ADC"));
        }

    }

}
